﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExcelReport
{
    public static class OperateXml
    {
        public static string FormatString(this FormConfigContent cell)
        {
            string _formula = cell.TemplateFormula;
            string[] cells = cell.FormCell.Split(',').ToArray();
            if (cell.FromSheet == cell.ToSheet)
            {
                if (string.IsNullOrWhiteSpace(cell.TemplateFormula))
                    _formula = "=" + cell.FormCell;
                //for (int i = 0; i < cells.Count; i++)
                //{
                //    _formula = _formula.Replace($"{i}", cells[i]);
                //}
                else
                {
                    _formula = string.Format(_formula, cells);
                }
            }
            else
            {
                if (string.IsNullOrWhiteSpace(cell.TemplateFormula))
                    _formula = "=" + cell.FromSheet + "!" + cell.FormCell;
                else
                {
                    cells = cells.Select(c => cell.FromSheet + "!" + c).ToArray();
                    //for (int i = 0; i < cells.Count; i++)
                    //{
                    //    _formula= string.Format(_formula, cell.FromSheet + "!" + cells[i]);
                    //}
                    _formula = string.Format(_formula, cells);
                }
            }
            return _formula;
        }
    }
}
